package com.starblink.android.basic.util.post

import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.starblink.android.basic.ext.formatDateString
import com.starblink.android.basic.user.UserRelationCodeEnum
import com.starblink.basic.autosize.utils.AutoSizeUtils.dp2px
import com.starblink.basic.ext.screenHeight
import org.joda.time.DateTime
import java.util.Calendar

object PostUtils {

    ///是否关注状态
    public fun followed(relationCode: Int): Boolean {
        return relationCode == UserRelationCodeEnum.FOLLOW.code || relationCode == UserRelationCodeEnum.BOTH.code
    }

    const val SECOND: Long = 1000
    const val MINUTE = SECOND * 60
    const val HOURS = MINUTE * 60
    const val DAY = HOURS * 24

    /**
     *  获取详细时间间隔 **天、小时、分钟、秒前
     */
    fun getTimeDiffDetailFriendly(dateTime: String): String {
        return getTimeDiffDetail(
            dateTime,
            " days ago",
            " hours ago",
            " minutes ago",
            " seconds ago"
        )

    }

    /**
     *  获取详细时间间隔 **天、小时、分钟、秒前
     */
    fun getTimeDiffDetail(
        dateTime: String,
        dayTip: String = "d",
        hourTip: String = "h",
        minTip: String = "m",
        secondTip: String = "s"
    ): String {
        var now = System.currentTimeMillis()
        ///2022-04-02T20:03:08.752+08:00
        var serverTime: Long = DateTime(dateTime).millis

        var timeDifference = now - serverTime
        var second = timeDifference / 1000;    //计算秒
        if (second < 60) {
            if (second < 1) {
                return "just";
            } else {
                //一分钟内用秒表示,显示：1s
                return "$second$secondTip"
            }
        } else {
            var minute = second / 60
            if (minute < 60) {
                //一小时以内用分钟表示，如23m
                return "$minute$minTip"
            } else {
                var hour = minute / 60
                if (hour < 48) {
                    //大于一小时小于两天内用小时表示:如3h
                    return "$hour$hourTip"
                } else {
                    var day = hour / 24
                    if (day > 5) {
                        //超过五天用日期 月日
                        return serverTime.formatDateString("MM-dd")
                    } else {
                        //大于两天小于等于5天用天表示，如3d
                        return "$day$dayTip"
                    }
                }
            }
        }
    }


    /**
     * 自动播放内容卡片视频
     */
    fun autoPlayPostVideo(
        layoutManager: StaggeredGridLayoutManager?,
        rv: RecyclerView,
        viewId: Int
    ) {
        //限定范围为屏幕一半的上下偏移200
        val playTop = screenHeight / 2 - dp2px(200f)
        val playBottom = screenHeight / 2 + dp2px(200f)
        //自动播放帮助类
        var scrollCalculatorHelper = ScrollCalculatorHelper(
            viewId,
            playTop,
            playBottom
        )

        rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            var firstVisibleItem = 0
            var lastVisibleItem = 0
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                scrollCalculatorHelper.onScrollStateChanged(recyclerView, newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                val positions =
                    layoutManager!!.findFirstVisibleItemPositions(null)
                if (positions != null && positions.isNotEmpty()) {
                    firstVisibleItem = positions[0]
                }
                val positions2 =
                    layoutManager!!.findLastVisibleItemPositions(null)
                if (positions2 != null && positions2.isNotEmpty()) {
                    lastVisibleItem = positions2[positions2.size - 1]
                }

                //这是滑动自动播放的代码
                scrollCalculatorHelper.onScroll(
                    recyclerView,
                    firstVisibleItem,
                    lastVisibleItem,
                    lastVisibleItem - firstVisibleItem
                )
            }
        })
    }
}